const {
	Controller
} = require('uni-cloud-router')
const uniID = require('uni-id')
const dbHelper = require('db-helper')
const consts = require('consts')
// const redis = uniCloud.redis()
module.exports = class GoodsController extends Controller {
	async getEvaluationPage() {
		let {
			scriptId,
			goodsId,
			storeId,
			pageNum = 1,
			pageSize = 10
		} = this.ctx.data
		if (!scriptId && !goodsId && !storeId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let page = await this.service.order.getEvaluationPage({
				scriptId,
				goodsId,
				storeId,
				pageNum,
				pageSize
			})
			return {
				code: 200,
				result: page
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	async getGoodsIndex() {
		let {
			city,
			token,
			userLocation,
			pageNum,
			pageSize
		} = this.ctx.data
		try {
			let result = await this.service.goods.getGoodsIndex({
				city,
				token,
				userLocation,
				pageNum,
				pageSize
			})
			return {
				code: 200,
				result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	async getGoodsList() {
		let {
			pageNum,
			pageSize,
			city,
			filter,
			order,
			keyword,
			userLocation,
			token
		} = this.ctx.data
		try{
			if (userLocation && (isNaN(parseFloat(userLocation.latitude)) || isNaN(parseFloat(userLocation.longitude)))) {
				userLocation = null
			}
			let startTime = Date.now()
			let res = await this.service.goods.getGoodsListPub({
				pageNum,
				pageSize,
				city,
				filter,
				order,
				keyword,
				userLocation,
				token
			})
			let endTime = Date.now()
			console.log('耗时：', endTime - startTime)
			return {
				code: 200,
				result: res
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	async getGoodsDetail() {
		let {
			id,
			userLocation,
			token
		} = this.ctx.data
		if (!id) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		if (userLocation) {
			if (isNaN(parseFloat(userLocation.latitude)) || isNaN(parseFloat(userLocation.longitude))) {
				userLocation = null
			}
		}
		return this.service.goods.getGoodsDetail(id, userLocation, token)
	}
	
	// 删除商品
	async delGoods() {
		let res = this.ctx.data
		if (!res) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		return this.service.goods.delGoods(res.goods_id)
	}
	
	//获取首页模块商品
	async getModuleList() {
		let {
			moduleId,
			pageNum,
			pageSize,
			city,
			userLocation,
			token,
			filter,
			order,
			keyword
		} = this.ctx.data
		try{
			let res = await this.service.goods.getModuleList({
				moduleId,
				pageNum,
				pageSize,
				city,
				userLocation,
				token,
				filter,
				order,
				keyword
			})
			return {
				code: 200,
				result: res
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//获取首页的上新和售罄商品
	async getNewGoodsList() {
		let {
			city,
			userLocation,
			token
		} = this.ctx.data
		try{
			let res = {
				code: 200,
				result: {
					newGoodList: [],
					saleEndGoodList: []
				}
			}
			console.log('consts', consts)
			let keyNewGoods = consts.redisKeys.keyNewGoods + city
			if (redis.exists(keyNewGoods)) {
				let newGoodsList = await redis.lrange(keyNewGoods, 0, await redis.llen(keyNewGoods))
				newGoodsList = newGoodsList.map(item => {
					return JSON.parse(item)
				})
				res.result.newGoodList = newGoodsList
			}
			let keySoldout = consts.redisKeys.keySoldout + city
			if (redis.exists(keySoldout)) {
				let soldoutList = await redis.lrange(keySoldout, 0, await redis.llen(keySoldout))
				soldoutList = soldoutList.map(item => {
					return JSON.parse(item)
				})
				res.result.saleEndGoodList = soldoutList
			}
			// let res = await this.service.goods.getNewGoodsList({
			// 	city,
			// 	userLocation,
			// 	token
			// })
			return res
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//获取限时秒杀商品
	async getSeckillGoodsList() {
		let {
			city,
			userLocation,
			token
		} = this.ctx.data
		try{
			let res = await this.service.goods.getSeckillGoodsList({
				city,
				userLocation,
				token
			})
			return {
				code: 200,
				result: res
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//获取首页轮播图
	async getHomeImageList() {
		let {} = this.ctx.data
		try{
			let res = await this.service.goods.getHomeImageList({})
			return {
				code: 200,
				result: res
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//秒杀倒计时结束后调用
	async getNextTime() {
		let {
			skuIdList
		} = this.ctx.data
		try{
			let res = await this.service.goods.getNextTime(skuIdList)
			return {
				code: 200,
				result: res
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//通过规格ID获取该规格的所有主题信息
	async getThemeDetail() {
		let {
			skuId,
			themeId
		} = this.ctx.data
		try{
			if (!skuId) {
				return {
					code: 500,
					msg: '参数错误'
				}
			}
			let res = await this.service.goods.getThemeDetail({
				skuId,
				themeId
			})
			return {
				code: 200,
				result: res
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//通过规格ID获取商品的图片名字价格
	async getPhotoAndPrice() {
		let {
			skuId
		} = this.ctx.data
		try{
			if (!skuId) {
				return {
					code: 500,
					msg: '参数错误'
				}
			}
			let res = await this.service.goods.getPhotoAndPrice({
				skuId
			})
			return {
				code: 200,
				result: res
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
}
